﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using PB.FBConnect;
using System.Web.Security;
using System.Web.UI.HtmlControls;

namespace PB.FBMembershipWebApp.Gen
{
    public partial class FBLogon : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            string ReturnUrl = Request.QueryString["ReturnUrl"];
            if (Request.IsAuthenticated)
            {
                if (!String.IsNullOrEmpty(ReturnUrl))
                {
                    Response.Redirect("~/Gen/UnauthorizedAccess.aspx?Url=" + Request.QueryString["ReturnUrl"]);
                }
                else
                {
                    Response.Redirect("~/Mem/Default.aspx");
                }
            }
            else
            {
                string fbQS = Request.QueryString["fb"];
                FBSession fbSession = new FBSession();
                if (fbQS != null && fbSession.IsConnected())
                {
                    bool succcess = FBMembership.LogonUser(fbSession.GetCurrentUser());
                    if (succcess == false)
                        Response.Redirect("/Gen/Lockout.aspx");

                }
                else
                {
                    HtmlGenericControl LoginDiv = LoginView1.FindControl("LoginDiv") as HtmlGenericControl;
                    if (LoginDiv != null)
                    {
                        LoginDiv.InnerHtml = "<fb:login-button size='large' background='white' length='long' onlogin='window.location=\"/Gen/FBLogon.aspx?fb=1&" + Request.QueryString.ToString() +"\";'></fb:login-button>";
                    }
                }
            }

        }
    }
}
